package com.lei2j.iterator;

public class MyList<T> {

	private Object[] elem;
	
	private int size;
	
	private int position;
	
	public MyList(int size) {
		if(size<0) {
			throw new IllegalArgumentException("size "+size+" < 0");
		}
		this.size = size;
		this.position = 0;
		this.elem = new Object[size];
	}
	
	public Iterator<T> iterator() {
		return new MyIterator();
	}
	
	public void add(T e) {
		elem[position++] = e;
	}
	
	public class MyIterator implements Iterator<T> {

		private int position;
		
		@Override
		public boolean hasNext() {
			return position < size;
		}

		@SuppressWarnings("unchecked")
		@Override
		public T next() {
			T t = (T) elem[position];
			position++;
			return t;
		}

		@Override
		public T remove() {
			throw new UnsupportedOperationException("");
		}
		
	}
}
